package com.leetcode.demo3Solution.JianOffer;

public class offer42 {
    public static void main(String[] args) {
        //连续子数组的最大和
       int[]  nums = {-2,1,-3,4,-1,2,1,-5,4};
       Solution42 solution42=new Solution42();
       System.out.println("连续子数组的最大和"+solution42.maxSubArray(nums));
    }
}

class Solution42 {
    public int maxSubArray(int[] nums) {
        if(nums.length==1) return nums[0];
        int max=0;
        int ans=nums[0];
        for(int i=0 ;i<nums.length;i++){
            if(max>0){
                max+=nums[i];
            }
            else {
                max=nums[i];
            }
            ans=Math.max(ans,max);

        }

        return ans;
    }
}
